package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApUnLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUnLikesBehaviorServiceImpl implements ApUnLikesBehaviorService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 存储不喜欢数据
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        // 1.校验参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUser = ApThreadLocalUtil.getUser();
        if (apUser == null || apUser.getId().equals(0)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 2.根据用户id和文章id查询redis
        String unLikeKey = "USER_UN_LIKES" + dto.getArticleId();
        Boolean unLikeFlag = redisTemplate.opsForHash().hasKey(unLikeKey, apUser.getId().toString());
        // 3.如果已执行不喜欢操作，并且仍要继续执行不喜欢该文章，则终止
        if (unLikeFlag && dto.getType() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "已不喜欢该文章，无法继续不喜欢");
        }
        // 4.如果已不喜欢，并且要取消不喜欢该文章，则删除redis和mongo
        if (unLikeFlag && dto.getType() == 1) {
            redisTemplate.opsForHash().delete(unLikeKey, apUser.getId().toString());
            mongoTemplate.remove(Query.query(
                    Criteria.where("userId").is(apUser.getId())
                            .and("articleId").is(dto.getArticleId())
            ), ApUnlikesBehavior.class);
        }
        // 5.如果没不喜欢，并且要取消不喜欢，则终止
        if (!unLikeFlag && dto.getType() == 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "没不喜欢该文章，无法取消不喜欢");
        }
        // 6.如果没不喜欢，并且要不喜欢该作者，则新增redis和mongo
        if (!unLikeFlag && dto.getType() == 0) {
            redisTemplate.opsForHash().put(unLikeKey, apUser.getId().toString(), "1");
            ApUnlikesBehavior apUnLikesBehavior = new ApUnlikesBehavior();
            apUnLikesBehavior.setUserId(Long.valueOf(apUser.getId()));
            apUnLikesBehavior.setArticleId(dto.getArticleId());
            apUnLikesBehavior.setType(dto.getType());
            apUnLikesBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apUnLikesBehavior);
        }
        return ResponseResult.okResult(200, "执行不喜欢该文章操作成功");
    }
}
